<?php
namespace app\admin\model;
use think\Request;
use think\Db;
use app\common\model\Common;
use app\staff\model\OrderCount;
use app\staff\model\RepairCount;
class Admin extends Common
{
	protected $append = ['sex_text','admin_type_text','shop_name','count_order','g_title','count_repair'];
	
	/**
	 * 获取员工列表
	 * @param  array $where  	查询条件
	 * @param  string $order  	排序条件
	 * @param  intger $page   	当前页数
	 * @param  intger $row    	每页条数
	 * @param  string $search 	检索条件
	 */
	public function getStaffList($where, $order, $page, $row, $search)
	{
		if($where === false) return [];
		if($row == 'limit'){
			return $this->alias('admin')
			->join('mps_access access','admin.admin_id  = access.admin_id','LEFT')
			->join('mps_group group','access.agid = group.agid','LEFT')
			->where($where)->where($search)->order($order)->group('admin.admin_id')->select();
		}else{
			return $this->alias('admin')
			->join('mps_access access','admin.admin_id  = access.admin_id','LEFT')
			->join('mps_group group','access.agid = group.agid','LEFT')
			->where($where)->where($search)->page($page, $row)->order($order)->group('admin.admin_id')->select();
		}
	}
	/**
	 * 获取员工列表
	 * @param  array $where  	查询条件
	 * @param  string $order  	排序条件
	 * @param  intger $page   	当前页数
	 * @param  intger $row    	每页条数
	 * @param  string $search 	检索条件
	 */
	public function getStaffCount($where, $order, $search)
	{
		if($where === false) return 0;
		return $this->alias('admin')
		->join('mps_access access','admin.admin_id  = access.admin_id','LEFT')
		->join('mps_group group','access.agid = group.agid','LEFT')
		->where($where)->where($search)->order($order)->count();
	}
	/**
	* 获取器
	*/
	public function getSexTextAttr($value, $data)
	{
		switch($data['sex']){
			case 0: return '保密';break;
			case 1: return '男';break;
			case 2: return '女';break;
			default: return '异常';break;
		}
	}
	public function getAdminTypeTextAttr($value, $data)
	{
		switch($data['admin_type']){
			case 0: return '总部';break;
			case 1: return '店长';break;
			case 2: return '营业员';break;
			case 3: return '维修员';break;
			default: return '异常';break;
		}
	}
	public function getAddtimeAttr($value)
	{
		return $this->date_time($value);
	}
	public function getShopNameAttr($value, $data)
	{
		return Db::name('shop')->where('shop_id',$data['shop_id'])->value('shop_name');
	}
	public function getCountOrderAttr($value, $data)
	{
		$OrderCount = new OrderCount();
		$where['order.admin_id'] = $data['admin_id'];
		$where['order.state'] = 1;
		return $OrderCount->countOrderGoods($where);
	}
	public function getGTitleAttr($value, $data)
	{
		return Db::name('access')->alias('a')->join('mps_group g','a.agid = g.agid','RIGHT')->where('a.admin_id',$data['admin_id'])->value('title');
	}
	public function getCountRepairAttr($value, $data)
	{
		$RepairCount = new RepairCount();
		$con = $RepairCount->oneRepairCount($data);
		return $con;
	}
}
